//
//  WKFoundWalletViewController1.m
//  ourToken
//
//  Created by wakeup on 2018/6/7.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKFoundWalletViewController1.h"
#import "WKBaseHeader.h"

@interface WKFoundWalletViewController1 ()

@property (weak, nonatomic) IBOutlet UITextField *name;//钱包名称
@property (weak, nonatomic) IBOutlet UITextField *password1;//密码
@property (weak, nonatomic) IBOutlet UITextField *password2;//再次确认密码
@property (weak, nonatomic) IBOutlet UILabel *mnemonicLabel;//助记词
@property (weak, nonatomic) IBOutlet UIButton *refreshBtn;//刷新按钮
@property (weak, nonatomic) IBOutlet UIButton *foundWalletBtn;//创建钱包按钮

@property (nonatomic, copy) NSString *serialNumber;//钱包序列号
@property (nonatomic, copy) NSString *address;//地址
@property (nonatomic, copy) NSString *private;//私钥

@end

@implementation WKFoundWalletViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    self.mnemonicLabel.text = [WKBlockChainManager refreshMnemonic];
    [self getWalletSerialNumberNetworking];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"创建%@钱包",self.coinType];
}

- (void)layoutUI {
    [self.refreshBtn setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
    
    self.foundWalletBtn.backgroundColor = AppManager.themeColor;
    self.foundWalletBtn.titleLabel.font = AppManager.btnFont;
    self.foundWalletBtn.layer.cornerRadius = 5;
    self.foundWalletBtn.layer.masksToBounds = YES;
    self.password1.secureTextEntry=YES;
    self.password2.secureTextEntry=YES;
}

//获取钱包序列号
- (void)getWalletSerialNumberNetworking {
    [httpManager getWalletSerialNumberSuccess:^(NSDictionary *result) {
        
        self.serialNumber = [result objectForKey:@"data"];
        
    } failure:^(NSError *error) {
        
    }];
}

//刷新助记词
- (IBAction)clickRefreshBtn:(id)sender {
    self.mnemonicLabel.text = [WKBlockChainManager refreshMnemonic];
}

//创建钱包
- (IBAction)clickFoundWalletBtn:(id)sender {
    
    //钱包A地址 mwhKYScWuYTJEDPTuSCQkjb75KafmU3XWc
    //钱包A私钥 cUPgytyiktxGhvQSwhwYTuKzPpam7kDLnoot4wSNYF4eDpUwE5db
    
    //钱包B地址 moSJChr3z4vRU3FrGFoVCGjGvxUmCmAiC1
    //钱包B私钥 cPQbgviKaWmg9YmwmXZiEQB8dFbXQf2ozzWy4f371h6FdY9ukpQD
    
    //钱包C地址 0xcace88fdb66588c537d48aa9acba88c5aad8a1bf
    //钱包C私钥 98254a992ec6df2fb0fd4a45f511a22ddf79f86328327b97507db5e6f9dc8ff4
    
    //钱包D地址 0x4b9d091a701bc54925099a918b20238c523dc0f6
    //钱包D私钥 cbdfb522975d4f8db3b195949717d920b354212f4a18511b07ea47366d9bff91
    
    NSArray *addressPrivateKeyArray;
    if ([self.coinType isEqualToString:@"BTC"]) {
        addressPrivateKeyArray = [WKBlockChainManager getPrivateAndAddressFormMnemonic:self.mnemonicLabel.text type:1];
    }
    else if ([self.coinType isEqualToString:@"ETH"]) {
        addressPrivateKeyArray = [WKBlockChainManager getPrivateAndAddressFormMnemonic:self.mnemonicLabel.text type:3];
    }
    
    self.address = addressPrivateKeyArray[0];
    self.private = addressPrivateKeyArray[1];
    [[NSUserDefaults standardUserDefaults] setObject:self.address forKey:@"地址"];
    [[NSUserDefaults standardUserDefaults] setObject:self.private forKey:@"私钥"];
    
    NSString *haha = [[NSUserDefaults standardUserDefaults] objectForKey:@"地址"];
    NSString *keke = [[NSUserDefaults standardUserDefaults] objectForKey:@"私钥"];
    DLog(@"地址 = %@",haha)
    DLog(@"私钥 = %@",keke)
    
    [httpManager foundWalletWithCoinType:self.coinType name:self.name.text address:self.address serialNumber:self.serialNumber createType:1 encryptedStr:self.mnemonicLabel.text success:^(NSDictionary *result) {
        
        if (self.isWelcome == YES) {
            WKRootViewController *root = [[WKRootViewController alloc] init];
            [self.navigationController pushViewController:root animated:YES];
        }
        else {
            
        }

    } failure:^(NSError *error) {
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_name resignFirstResponder];
    [_password1 resignFirstResponder];
    [_password2 resignFirstResponder];
}

@end
